<template>
  <el-dialog :title="dialogTitle" :visible.sync="visible" width="50%" @close="handleClose">
    <el-form :model="form" label-width="160px" ref="orderFormRef" :rules="rules">
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="工单编号" prop="orderId">
            <el-input v-model="form.orderId" :disabled="dialogTitle === '编辑订单'"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="合同年限" prop="contractYears">
            <el-input v-model="form.contractYears"></el-input>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="单位" prop="unit">
            <el-input v-model="form.unit"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="合同有效期" prop="validityPeriod">
            <el-date-picker
              v-model="form.validityPeriod"
              type="date"
              placeholder="选择日期"
              style="width: 100%;"
            ></el-date-picker>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="地区" prop="region">
            <el-input v-model="form.region"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="合同金额" prop="amount">
            <el-input v-model="form.amount"></el-input>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="专线产品实例标示" prop="productInstanceId">
            <el-input v-model="form.productInstanceId"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="客户经理名称" prop="managerName">
            <el-input v-model="form.managerName"></el-input>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="客户经理联系电话" prop="managerPhone">
            <el-input v-model="form.managerPhone"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="专线数量" prop="lineCount">
            <el-input v-model="form.lineCount"></el-input>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="客户名称" prop="customerName">
            <el-input v-model="form.customerName"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="客户联系电话" prop="customerPhone">
            <el-input v-model="form.customerPhone"></el-input>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="订购数量/次数/时长" prop="quantity">
            <el-input v-model="form.quantity"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="合同编号" prop="contractNumber">
            <el-input v-model="form.contractNumber"></el-input>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="合同附件地址" prop="attachmentUrl">
            <el-input v-model="form.attachmentUrl"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="计费方式" prop="billingMethod">
            <el-input v-model="form.billingMethod"></el-input>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="生效方式" prop="effectiveMethod">
            <el-input v-model="form.effectiveMethod"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="handleCancel">取 消</el-button>
      <el-button type="primary" @click="handleSubmit">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  name: "OrderSyncForm",
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    dialogTitle: {
      type: String,
      default: ""
    },
    formData: {
      type: Object,
      default: () => ({})
    }
  },
  data() {
    return {
      form: { ...this.formData },
      rules: {
        orderId: [{ required: true, message: '请输入工单编号', trigger: 'blur' }],
        contractYears: [{ required: true, message: '请输入合同年限', trigger: 'blur' }],
        unit: [{ required: true, message: '请输入单位', trigger: 'blur' }],
        validityPeriod: [{ required: true, message: '请选择合同有效期', trigger: 'change' }],
        region: [{ required: true, message: '请输入地区', trigger: 'blur' }],
        amount: [{ required: true, message: '请输入合同金额', trigger: 'blur' }],
        productInstanceId: [{ required: true, message: '请输入专线产品实例标示', trigger: 'blur' }],
        managerName: [{ required: true, message: '请输入客户经理名称', trigger: 'blur' }],
        managerPhone: [{ required: true, message: '请输入客户经理联系电话', trigger: 'blur' }],
        lineCount: [{ required: true, message: '请输入专线数量', trigger: 'blur' }],
        customerName: [{ required: true, message: '请输入客户名称', trigger: 'blur' }],
        customerPhone: [{ required: true, message: '请输入客户联系电话', trigger: 'blur' }],
        quantity: [{ required: true, message: '请输入订购数量/次数/时长', trigger: 'blur' }],
        contractNumber: [{ required: true, message: '请输入合同编号', trigger: 'blur' }],
        attachmentUrl: [{ required: true, message: '请输入合同附件地址', trigger: 'blur' }],
        billingMethod: [{ required: true, message: '请输入计费方式', trigger: 'blur' }],
        effectiveMethod: [{ required: true, message: '请输入生效方式', trigger: 'blur' }]
      }
    };
  },
  methods: {
    handleSubmit() {
      this.$refs.orderFormRef.validate((valid) => {
        if (valid) {
          this.$emit('submit', { ...this.form });
          this.$message.success('操作成功')
          this.handleCancel();
        } else {
          console.log('表单验证失败!');
          return false;
        }
      });
    },
    handleCancel() {
      this.$emit('update:visible', false);
    },
    handleClose() {
      this.$emit('update:visible', false);
    }
  },
  watch: {
    formData: {
      handler(newVal) {
        this.form = { ...newVal };
      },
      deep: true
    }
  }
};
</script>

<style scoped lang="scss">
/* 样式可以根据需要自定义 */
</style>
